<template>
  <div id="login-bg">
    <div id="login">
      <div id="message">管理登录</div>
      <div id="shadow"></div>
      <el-form ref="form" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="账号" prop="account">
          <el-input placeholder="请输入账号" prefix-icon="fa fa-user-o" v-model="form.account"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input placeholder="请输入密码" prefix-icon="fa fa-lock" show-password v-model="form.pwd"> </el-input>
        </el-form-item>
        <el-form-item style="float: right">
          <el-button type="primary" @click="submit('form')">登录</el-button>
          <el-button @click="reset('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { login } from '@/network/api.js'
export default {
  name: 'login',
  data() {
    return {
      form: {
        account: '',
        pwd: ''
      },
      rules: {
        account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        pwd: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  created() {},
  methods: {
    // 提交表单
    submit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            const rtn = await login(this.form)
            this.$storage.set('token', rtn.data.token, rtn.data.expires_time)
            this.$storage.set('info', rtn.data.roleinfo, rtn.data.expires_time)
            this.$router.push('/home')
          } catch (e) {
            this.$message.error(e.message)
          }
        } else {
          return false
        }
      })
    },
    // 重置表单
    reset(formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang="scss" scoped>
#login-bg {
  background: url(~@/assets/images/bg.png) no-repeat center;
  background-size: cover;
  overflow: hidden;
  height: 100vh;
}
#login {
  margin: 120px auto 0 auto;
  min-height: 420px;
  max-width: 420px;
  padding: 40px;
  background-color: #ffffff;
  margin-left: auto;
  margin-right: auto;
  border-radius: 4px;
  /* overflow-x: hidden; */
  box-sizing: border-box;
  #message {
    margin: 10px 0 0 -58px;
    padding: 18px 10px 18px 60px;
    /* background: #189F92; */
    background: #409eff;
    position: relative;
    color: #fff;
    font-size: 16px;
  }
  #shadow {
    background: url(~@/assets/images/aiwrap.png);
    width: 18px;
    height: 10px;
    margin: 0 0 20px -58px;
    position: relative;
  }
}
</style>
